package com.higgins.server.agentapp.datamodel;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.higgins.client.agentapp.datamodel.AdvertisedJobDTO;
import com.higgins.client.agentapp.datamodel.CorrespondenceDTO;
import com.higgins.client.dataModel.StrategyDTO;

@Entity
@Table(name = "correspondence")
public class Correspondence {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
	private Long id;
    
    private String description; 
    
    @ManyToOne
    @JoinColumn(name="advertisedJobId")
    private AdvertisedJob advertisedJob;

    @ManyToOne
    @JoinColumn(name="agentId")
    private Agent agent;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="timestamp")
    private Date timestamp;
    

    public Correspondence() {
        // This is used by JPA
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
    
    public AdvertisedJob getAdvertisedJob()	{
    	return advertisedJob;
    }
    
    public void setAdvertisedJob(AdvertisedJob advertisedJob)	{
    	this.advertisedJob = advertisedJob;
    }

    public Agent getAgent()	{
    	return agent;
    }    
    public void setAgent(Agent agent)	{
    	this.agent = agent;
    }

    public CorrespondenceDTO toDTO()	{
    	CorrespondenceDTO dto = new CorrespondenceDTO();
    	dto.setId(id);
    	dto.setDescription(description);
    	dto.setTimestamp(timestamp);
    	if (advertisedJob!=null)	{
    		dto.setAdvertisedJobDTO(advertisedJob.toDTO());
    	}
    	if (agent!=null)	{
    		dto.setAgentDTO(agent.toDTO());
    	}
    	return dto;
    }
    public void update(CorrespondenceDTO dto)	{
    	this.description = dto.getDescription();
    	this.timestamp = dto.getTimestamp();
    	if (dto.getAdvertisedJobDTO()!=null)	{
    		// Need to update the covering letter here!!
    	} else {
    		advertisedJob = null;
    	}
    	if (dto.getAgentDTO()!=null)	{
    		// Need to update the covering letter here!!
    	} else {
    		agent = null;
    	}
    }

}